package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.DesignDAO;
import com.daxia.bumall.dao.DesignProductDAO;
import com.daxia.bumall.dto.DesignDTO;
import com.daxia.bumall.dto.DesignProductDTO;
import com.daxia.bumall.model.Design;
import com.daxia.bumall.model.DesignProduct;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class DesignProductService {
	
	@Autowired
	private DesignProductDAO designProductDAO;
	@Autowired
	private DesignService designService;
	
	public List<DesignProductDTO> find(DesignProductDTO query, Page page) {
		List<DesignProduct> models = designProductDAO.find(query, page);
		List<DesignProductDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<DesignDTO> findDistictDesigns(DesignProductDTO query,Page page) {
		List<Design> designs = designProductDAO.findDistictDesigns(query, page); 
		List<DesignDTO> designDTOs = new Vector<DesignDTO>();
		if(designs != null) {
			designDTOs = designService.toDesignDTOs(designs);
		}
		return designDTOs;
	}
	
	public List<DesignProductDTO> count(DesignProductDTO query, Page page) {
		List<DesignProduct> models = designProductDAO.count(query, page);
		List<DesignProductDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<DesignProductDTO> findAll() {
		return this.find(new DesignProductDTO(), null);
	}
	
	public List<DesignProductDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new DesignProductDTO(), page);
	}
	
	public Long create(DesignProductDTO dto) {
		DesignProduct model = new DesignProduct();
		toModel(model, dto);
		return designProductDAO.create(model);
	}
	
	public DesignProductDTO load(Long id) {
	    DesignProduct model = designProductDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(DesignProductDTO dto) {
		DesignProduct model = designProductDAO.load(dto.getId());
		toModel(model, dto);
		designProductDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				designProductDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public DesignProductDTO findOne(DesignProductDTO query) {
		DesignProduct model = designProductDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<DesignProductDTO> toDTOs(List<DesignProduct> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DesignProductDTO>(0);
		}
		List<DesignProductDTO> dtos = new ArrayList<DesignProductDTO>(models.size());
		for (DesignProduct model : models) {
	        DesignProductDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private DesignProductDTO toDTO(DesignProduct model) {
		if (model == null) {
			return null;
		}
		DesignProductDTO dto = BeanMapper.map(model, DesignProductDTO.class);
		
		return dto;
	}
	
	private void toModel(DesignProduct model, DesignProductDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<DesignProduct> toModels(List<DesignProductDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<DesignProduct>(0);
		}
		List<DesignProduct> models = new ArrayList<DesignProduct>(dtos.size());
		for (DesignProductDTO dto : dtos) {
	        DesignProduct model = new DesignProduct();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}

	public void deleteByDesignId(Long designId) {
		designProductDAO.deleteByDesignId(designId);
	}

	public List<DesignProductDTO> findByDesign(Long designId) {
		DesignProductDTO query = new DesignProductDTO();
		query.setDesign(new Design());
		query.getDesign().setId(designId);
		return this.find(query, null);
	}
}
